package com.fairytask.ui.item
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	
	/**
	 * 时间显示组件 
	 * @author zhuy
	 * 
	 */	
	public class TimeShow extends Sprite
	{
		private var timeTxt:TextField;
		private var _tf:TextFormat;
		private var _time:int;
		private var _timer:Timer;
		private var _filter:GlowFilter;
		private var _style:int;//时间显示类型: 0: 00 : 00 : 00   1:00:00:00  2:00:00(不显示时，只有分和秒)
		public function TimeShow(secAll:int=0,color:String="ffa800",filter:GlowFilter = null,style:int=0)
		{
			super();
			timeTxt = new TextField();
			if(style == 2)
			{
				timeTxt.width = 35;
			}else
			{
				timeTxt.width = 80;
			}
			
			timeTxt.height= 20;
			_tf			=	new TextFormat();
			_tf.font	=	"微软雅黑";
			timeTxt.defaultTextFormat	=	_tf;
			_style = style;
			if(null == filter)
			{
				_filter = new GlowFilter(0x000000,1,2,2,6,BitmapFilterQuality.LOW);
			}else
			{
				_filter = filter;
			}
			timeTxt.selectable = false;
			timeTxt.filters = [_filter];
			timeTxt.textColor = uint("0x" + color);
			addChild(timeTxt);
			if(secAll!=0)
			{
				_time = secAll;
				styleChange(secAll);
				_timer = new Timer(1000,_time);
				initEvent();
			}
		}
		
		/**
		 * 设置timer时间，是否启动 
		 * @param secAll
		 * @param needStart
		 * 
		 */		
		public function setTimer(needStart:Boolean=true,secAll:int=0):void
		{
			if(secAll<=0)
			{
				styleChange(secAll);
				return;
			}
			_time = secAll;
			styleChange(secAll);
			if(!_timer)
			{
				_timer = new Timer(1000,_time);
			}
			_timer.reset();
			_timer.repeatCount = _time;
			if(needStart)
			{
				if(!_timer.hasEventListener(TimerEvent.TIMER))
				{
					initEvent();
				}
				_timer.start();
			}
		}
		
		/**
		 * 手动开始倒计时 
		 * 
		 */
		public function startTime():void
		{
			if(!_timer.hasEventListener(TimerEvent.TIMER))
			{
				initEvent();
			}
			_timer.start();
		}
		
		public function stopTime():void
		{
			if(_timer)
			{
				_timer.stop();
			}
		}
		
		public function setFilter(filter:GlowFilter):void
		{
			_filter = filter;
			timeTxt.filters = [_filter];
		}
		
		/**
		 * 重新初始化
		 */		
		public function reInit():void
		{
			if(_timer)
			{
				clearEvent();
				_timer = null;
			}
			_time = 0;
			if(_style == 0)
			{
				timeTxt.text = "00 : 00 : 00";
			}
			if(_style == 1)
			{
				timeTxt.text = "00:00:00";
			}
			if(_style == 2)
			{
				timeTxt.text = "00:00";
			}
		}
		
		/**
		 * 设置文字格式 
		 * @param textFormat
		 * 
		 */
		public function set textFormat(textFormat:TextFormat):void
		{
			timeTxt.setTextFormat(textFormat);
			timeTxt.defaultTextFormat = textFormat;
		}
		
		private function initEvent():void
		{
			_timer.addEventListener(TimerEvent.TIMER,onTimerCount);
			_timer.addEventListener(TimerEvent.TIMER_COMPLETE,onTimerOver);
			if(_time>0)
			{
				_timer.start();
			}
		}
		
		private function clearEvent():void
		{
			_timer.removeEventListener(TimerEvent.TIMER,onTimerCount);
			_timer.removeEventListener(TimerEvent.TIMER_COMPLETE,onTimerOver);
			_timer.stop();
		}
		
		public function get time():int
		{
			return _time;
		}
		
		private function onTimerCount(evt:TimerEvent):void
		{
			if(_time>=1)
			{
				_time--;
				styleChange(_time);
			}
		}
		
		private function onTimerOver(evt:TimerEvent):void
		{
			_timer.stop();
			dispatchEvent(evt);
		}
		
		/**
		 * 把后台的数据按照:02:02:59的格式显示
		 * @param sec
		 * 
		 */		
		public function styleChange(secAll:int):void
		{
			var hour:int = secAll/3600;
			var hourStyle:String;
			var minute:int = (secAll - hour*3600)/60;
			var minuteStyle:String;
			var sec:int  = secAll - hour*3600 -60*minute;
			var secStyle:String;
			if(hour<=9)
			{
				hourStyle = "0"+String(hour);
			}
			else
			{
				hourStyle = String(hour);
			}
			
			if(minute<=9)
			{
				minuteStyle = "0"+String(minute); 
			}
			else
			{
				minuteStyle = String(minute);
			}
			
			if(sec<=9)
			{
				secStyle  = "0"+String(sec);
			}
			else
			{	
				secStyle  = String(sec);
			}
			
			if(_style == 0)
			{
				timeTxt.text = hourStyle + " : " + minuteStyle + " : " + secStyle;
			}
			else if(_style == 1)
			{
				timeTxt.text = hourStyle + ":" + minuteStyle + ":" + secStyle;
			}	
			else if(_style == 2)
			{
				timeTxt.text = minuteStyle + ":" + secStyle;
			}
			timeTxt.dispatchEvent(new Event(Event.CHANGE ));
		}
		
		
		/**
		 * 显示颜色
		 * 
		 */		
		public function styleColor(val:String):void
		{
			timeTxt.textColor = uint("0x" + val);
			timeTxt.text = timeTxt.text;
		}
		
		public function set time(val:int):void
		{
			if(val<=0)
			{
				_time = 0;
				styleChange(_time);
				return;
			}
			if(val>0)
			{
				_time = val;
				styleChange(_time);
				if(null == _timer)
				{
					_timer = new Timer(1000);
					initEvent();
				}
				_timer.repeatCount = _time;
				_timer.reset();
				_timer.start();
			}
		}
		
		public function set timeVal(val:int):void
		{
			_time = val;
			styleChange(_time);
		}
		
		public function get timeTextField():TextField
		{
			return timeTxt;
		}
	}
}